<template>
    <div class="wrapper" ref="wrapper">
        <div class="content">
            <slot></slot>
        </div>
    </div>
</template>

<script>
    import BScroll from 'better-scroll'
    export default {
        name: "Scroll",
        props:{
            probeType:{
                type:Number,
                default:0
            },
            pullUpLoad:{
                type:Boolean,
                default:false
            }
        },
        data(){
            return {
                scroll:null
            }
        },
        mounted() {
            /**
             * ref如果是绑定在组件上时，那么通过this.$refs.ref名字，获取到的是该组件【对象】
             * ref如果是绑定在普通的元素上，那么通过this.$refs.ref名字 获取到的是一个元素对象
             */
             this.$nextTick(()=>{
                //创建better-scroll对象
                this.scroll = new BScroll(this.$refs.wrapper,{
                    click:true,
                    probeType:this.probeType,
                    pullUpLoad:this.pullUpLoad
                });
                //实时监听滚动的位置
                if (this.probeType == 2 || this.probeType == 3) {
                    this.scroll.on('scroll',position=>{
                        this.$emit('scroll',position)
                    });
                }
                //监听scroll滚动到底部上拉事件
                if (this.pullUpLoad) {
                    this.scroll.on('pullingUp',()=>{
                        this.$emit('pullingUp')
                    })
                }
             })
        },
        methods:{
            scrollTo(x,y,time=300) {
                //父组件拿该子组件的this.scroll的时候可能它还没有初始化出来
                this.scroll && this.scroll.scrollTo && this.scroll.scrollTo(x,y,time)
            },
            finishPullUp() {
                this.scroll && this.scroll.finishPullUp && this.scroll.finishPullUp()
            },
            refresh() {
                // console.log('222')
                this.scroll && this.scroll.refresh && this.scroll.refresh()
            },
            getScrollY() {
                return this.scroll?this.scroll.y:0;
            }
        }
    }
</script>

<style scoped>

</style>